//8033b23c = timer for time stop
//8033b240 = ptr to function to run during time stop
//above are for complete pause of everything

//8033d480 = if it has byte 0x2 it starts time stop without mario being //frozen. [2=freeze everything but mario, or 0x200000 to 0x74 in obj struct]

//80384678

//80385c00

//8029cbc8

//8029cd28

//above functions are just chain of behavior parsing I used to look at this

//802D77DC
//PrintGenericText
//[A0 = int left, A1 = int bottom, A2 = pointer //sm64_string]


//jal 0x802d7384 //ortho matrix
//changes global to screen coords

//create translation matrix - 802d7070
//a0=matrix type [1 = push matrix, 2 = multiply matrix]
//a1=x translation as float
//a2=y translation as float
//a3=z translation as float

//create matrix - 802d7280
//a0=matrix type [1 = push matrix, 2 = multiply matrix]
//a1=x scale as float
//a2=y scale as float
//a3=z scale as float

//handles menu input with control stick - 802d862c
//a0=input type[1=vertical,2=horizontal]
//a1=*menuvalue
//a2=minvalue
//a3=maxvalue

//to create a text that scrolls one character at a 
//time just write the string one character at a time. 
//To give the appearance of a textbox jut make your 
//own texrect at the correct location.
//add some controls inside the actual text data for 
//pauses and emphasis.

//text color:
//just use an env color right before it will color 
//the entire string. so basically, get end of master 
//DL - 8033b06c, store an env color there, update the 
//end of master DL ptr.

//b8 for master DL is written at 0x80247d14, only gives black box on jabo

//hook at 0x8027ba80 works for jabo for some reason (draw shadows function)

//hook at 8027806c is after all the movewords that start the master DL, none work

//80404ffC = lowercase

//8040505C = uppercase

/*/
keyboard sets:
lower:
00 9E 01 9E 02 9E 03 9E 04 9E 05 9E 06 9E 07 9E 08 9E 09 FE 34 9E 3A 9E 28 9E 35 9E 37 9E 3C 9E 38 9E 2C 9E 32 9E 33 FE 24 9E 36 9E 27 9E 29 9E 2A 9E 2B 9E 2D 9E 2E 9E 2F 9E E6 FE 3D 9E 3B 9E 26 9E 39 9E 25 9E 31 9E 30 9E E5 9E F4 9E 52 FE 50 9E 9E 9E 1C 19 0A 0C 0E 9E 9E 9E 0E 17 0D FF 



00 9E 01 9E 02 9E 03 9E 04 9E 05 9E 06 9E 07 9E 08 9E 09 FE 1A 9E 20 9E 0E 9E 1B 9E 1D 9E 22 9E 1E 9E 12 9E 18 9E 19 FE 0A 9E 1C 9E 0D 9E 0F 9E 10 9E 11 9E 13 9E 14 9E 15 9E E6 FE 23 9E 21 9E 0C 9E 1F 9E 0B 9E 17 9E 16 9E E5 9E F4 9E 52 FE 50 9E 9E 9E 1C 19 0A 0C 0E 9E 9E 9E 0E 17 0D FF
/*///


.orga 0x1204ffc
.incbin "keyboard.bin"


//40 to 4F is free
//70 to 9d is free
//use above numbers for special effects such as sounds and colors
//cutscenes/time stop will not enabled for this, that will have to be done seperately


//change 0xb9 value at 0x02011cc8 have CVG_X_ALPHA enabled so it works on pj64 video... why (not sure if will be needed in final)

//special text encoding cmds

//first is cmd byte, then parameter byte if necessary

//done
//40 set VIs per character, 2 byte - VIs per new char
//41 play sfx, 2 bytes - sfx arg
//42 set color of following characters, 4 bytes (white by default) - RGBA
//43 display short user string, 1 byte - user string number (each string is 10 bytes and goes from 6f00 to 6FF0, so inputs are 0 to F)
//44 display long user inputted string, up to FF characters
//45 return from user string
//46 make rainbow text (characters following cmd will rgb glow, call a new env color to disable) 1 byte, alpha value
//47 translate origin, 4 bytes - 2 bytes x pos, 2 bytes y pos
//48 jump string, 4 bytes - ram address
//4C enable A press to increase text speed, 2 bytes - speed while holding A
//4D disable A press to jump to end of box
//4E re enable user input on re-talk (by default, a user input only works once, then the cmd acts as if it is not there, ofc this does not stay after resetting the game)
//4F allow user input of short string, 1 byte - short user string ID. (This cmd will remove all other input and bring a keyboard up the player can type on. If you are not in time stop mario will move around while typing and it will suck)
//boxes will reset scaling and position and color. The only thing preserved is the character speed, and the blip flags.
//70 end of box, auto go to next box (put a pause before this so your string stays a while)
//71 end of box, goto next box on a press
//72 remove all text for certain number of VIs, 2 bytes - num of VIs with no text (Text will flash back to its previous spot after it is done, so if you had three sentences, it will auto go back to that point once the cmd is done), 0 has no effect
//74 pause - 2 bytes VIs to wait
//76 play small blip sound per character (think like in megaman or any other rpg with text scrolling) call again to disable
//77 disable blip
//78 change music that plays after message to currently playing music (basically use this so if you switch tracks mid message, the track keeps playing, use after play music cmd)
//79 play music track, 1 byte - track number (original music will resume when string is done)
//7B stop displaying string on A press (basically use this before an FF)
//7C stop displaying string after number of VI's (basically use this before an FF) - 2 bytes - num of VI's to wait until string is gone
//7F define background box, 2 bytes x start, 2 bytes y start, 2 bytes x end, 2 bytes y end. 4 bytes env color (coordinates go from the bottom right as the ori gin and increase)
//84 scale text, 4 bytes x scale float, 4 bytes y scale float (To compensate for large Y scaling, just do several line breaks the text will go farther up, e.g. y scale of 2 will need an empty line above it to not overlap, x will auto align)
//85 enable dialog options, 1 byte, number of options (they will be displayed vertically), the following strings will be the options, to use an user inputted string as an option do this (ex. 43 FF), returns byte of option selected (zero indexed)
//86 display following only on returned dialog option, 1 byte return, truncate string with FF, if return value does not match it will skip to following string, if there is no following string it will just display nothing (zero indexed)
//87 display general text (basically anything after this will display no matter what dialog option you choose, if you put another 86 cmd after this, general text will end and it will pass all text until another 87 or a matching 86)
//88 enable screen shake
//89 disable screen shake

//not done

//don't know if I want to add these, maybe in an update or something


//too lazy right now to add

//80 define textured background box, 2 bytes width, 2 bytes height, 2 bytes alpha, 4 bytes texture ptr (coords are increasing, aka it will start 12 before the text start in both directions and increase by the param amount)

//81 disable all background boxes



//done
//9D is padding, aka just gets passed (use this to blank out a cmd with asm or something)
//FE is line break
//FF is end of string, this is used for dialog options and such. Use 7B and 7C to end full string


//store temporary text starting from 0x807f7000
//6d00 VI of last character
//6d04 env color
//6d08 VI's per character
//6d0a will store state of keyboard input re input
//6d0c will store temp string ptr
//6d10 will store temp x
//6d12 will store temp y
//6d14 will store temp string end pos
//6d18 will store ptr to sfx
//6d1a will store check if blip is playing
//6d1c will store return ptr for user inputted string
//6d20 will store original music track
//6d22 will store switched to music
//6d24 will store VI timer for no display
//6d28 will store VI timer for string end
//6d2C will store VI timer for pause
//6d30 will store x scale
//6d34 will store y scale
//6d38 will store env color
//6d3C will store color for rainbow text
//6d40 will store string ptr for text removal
//6d44 will store flag for user input
//6d45 will store whether shift key is used
//6d46 will store which letter is being moved to
//6d47 will store which letter is selected
//6d48 will store VI timer for keyboard input
//6d4c will store ptr for keyboard removal
//6d50 will store location of user input
//6d54 will store number of lowercase letters
//6d58 will store returned dialog option
//6d59 will store currently displaying dialog option
//6d5A will store total number of options
//6d5B will store hovered dialog option
//6d5C will store ptr to end of all dialog options
//6d60 will store screen shake byte
//6d62 will store counter for VI display
//6d64 will store ptr to end of text in DL
//6d68 will store original pointer to string for buffer offset
//6d6C will store speed set with 40 if replaced

//6df0 will be used as current position in the string
//6df4 will store x coord
//6df6 will store y coord
//6df8 will store the original string
//6dfc will store string end pos


//6000 will store cmd buffer (prob oversized)
//6e00 will store long string, up to FF characters (4E truncated)
//6f00 to 6ff0 - user inputted strings, 15 characters each (4E truncated)

//kerning for upper and lowercase letters is different, add variable that accounts for that, 5 is for lowercase, 7 for uppercase
//use VI to time string

//super string controller function
.orga 0x1203800

addiu sp, sp, -0x20
sw ra, 0x14 (SP)

//check if there is a string, if not do nothing
lui s7, 0x807f
lw a2, 0x6df8 (S7) //ptr string
beq a2, r0, end
sw a2, 0x6d0c (S7)

lui at, 0x8034
lw v0, 0xb06c (AT) //current end of master DL
lw t0, 0x6d64 (S7) //end of last text
beq t0, v0, end
lh at, 0x6d62 (S7)
addiu t1, at, 1
beq at, r0, end
sh t1, 0x6d62 (S7)

//if there is a string, set it up
jal 0x80404d68 //insert setup ptr
sw r0, 0x6d54 (S7)
lh a0, 0x6df4 (S7) //start x pos
lh a1, 0x6df6 (S7) //start y pos
sh a0, 0x6d10 (S7) //temp x pos
sh a1, 0x6d12 (S7) //temp y pos
lw at, 0x6dfc (S7) //string end pos
sw at, 0x6d14 (S7) //temp string end pos
sh r0, 0x6d18 (S7) //sfx ptr
lui at, 0x3f80
sw at, 0x6d30 (S7) //x scale
sw at, 0x6d34 (S7) //y scale
subiu at, r0, 0x1
sw at, 0x6d38 (S7) //env color



lw s6, 0x8032D580 //current VI





lw at, 0x6d24 (S7)
bne at, r0, nodisplayonVI

lw t1, 0x6d44 (S7)
bne t1, r0, makeuserinput

//next parse string for special cmds and characters
parse:
lw a2, 0x6d0c (S7)
lw t1, 0x6df0 (S7) //current pos in string
addu t2, t1, a2
lbu t2, 0x0 (T2) //part in string to parse
sltiu t3, t2, 0x40 //1 if its less than 40
subiu t4, t2, 0x50
sltiu t4, t4, 0x20 //1 if its between 50 and 6f
subiu t5, t2, 0xd0
sltiu t5, t5, 0x2E //1 if its greater than D0
subiu t7, t2, 0x9e
sltiu t7, t7, 0x2
or t3, t3, t7 //catches space and dash
or t6, t4, t3 //will be 1 if its a normal character
or t5, t6, t5
bne t5, r0, normalchar
nop
//from here I will parse special cmds

lw t9, 0x6df0 (S7) //current pos in string 
lw t8, 0x6d0c (S7)
addu t8, t9, t8
lbu t7, 0x0 (T8) //char in string to read

li t3, 0xff
beq t3, t7, endofstring
li t2, 0xfe
beq t2, t7, linebreak

//should use a jump table for every other special

li t3, 0x40
beq t3, t7, setspeed
li t2, 0x42
beq t2, t7, setenvcolor
li t3, 0x70
beq t3, t7, AutoBoxEnd
li t2, 0x71
beq t2, t7, ABoxEnd
li t3, 0x41
beq t3, t7, setsfx
li t2, 0x44
beq t2, t7, longuserstring
li t3, 0x45
beq t3, t7, returnuserstring
li t2, 0x76
beq t2, t7, enableblip
li t3, 0x79
beq t3, t7, playmusictrack
nop
li t3, 0x7B
beq t3, t7, removestringonA
li t2, 0x7C
beq t2, t7, removestringonVI
li t3, 0x74
beq t3, t7, pauseforVI
li t2, 0x77
beq t2, t7, disableblip
li t3, 0x84
beq t3, t7, scaletext
li t2, 0x7f
beq t2, t7, drawshadebox
li t3, 0x43
beq t3, t7, shortuserstring
li t2, 0x72
beq t2, t7, nodisplayonVI
li t3, 0x9D
beq t3, t7, blankout
li t2, 0x46
beq t2, t7, enablerainbowtext
li t3, 0x4f
beq t3, t7, userinputshortstring
li t2, 0x78
beq t2, t7, changeoriginalmusic
li t3, 0x85
beq t3, t7, startdialogoptions
li t2, 0x86
beq t2, t7, displayoncorrectdialogreturn
li t3, 0x88
beq t3, t7, shakescreen
li t2, 0x89
beq t2, t7, disablescreenshake
li t3, 0x4e
beq t3, t7, enablereinput
li t2, 0x87
beq t2, t7, blankout
li t3, 0x4c
beq t3, t7, speeduptext
li t2, 0x4d
beq t2, t7, disablefasttext
li t3, 0x47
beq t3, t7, translateorigin
li t2, 0x48
beq t2, t7, jumptext
nop

//42 cmd
setenvcolor:
jal 0x80404e00 //set env color
sw t8, 0x18 (SP)
lh a0, 0x6d10 (S7)
lh a1, 0x6d12 (S7)
ori a2, s7, 0x7000
jal 0x802d77dc //print string
sw t8, 0x18 (SP)
lw t8, 0x18 (SP)
jal 0x80404dd0 //flushstring
andi t9, t8, 0x3 //unaligned address mod 4
addiu t9, t9, 0x1
sll t9, t9, 3
subiu at, r0, 0x1
srlv t1, at, t9
sllv at, at, t9
LWR t7, 0x4 (T8)
and t7, t7, t1
LWL t8, 0x1 (T8)
and t8, t8, at
or a0, t7, t8


jal 0x80404e00 //set env color
sw a0, 0x6d38 (S7)


jal 0x80405b0c //update x pos of string
nop
jal 0x80405118 //pop matrix
nop
jal 0x80405138 //push scale matrix
nop
jal 0x804051c0 //fix translation after scale
nop

lw t9, 0x6d0c (S7) //temp string
lw t1, 0x6df0 (S7) //current pos in string

lw t2, 0x6d14 (S7)
subu t2, t2, t1
sw t2, 0x6d14 (S7)
sw r0, 0x6df0 (S7)
sw r0, 0x6d54 (S7)
addu t9, t1, t9 //last byte read
addiu t9, t9, 0x5
beq r0, r0, parse
sw t9, 0x6d0c (S7)


//46 cmd
enablerainbowtext:
//print previous string
jal 0x80404e00 //set env color
sw t8, 0x18 (SP)
lh a0, 0x6d10 (S7)
lh a1, 0x6d12 (S7)
jal 0x802d77dc //print string
ori a2, s7, 0x7000
jal 0x80404dd0 //flushstring
nop



//set rainbow color for next string
lw t3, 0x6d3C (S7)
bne t3, r0, oscillate
lw t4, 0x6d38 (S7)
sw t4, 0x6d3C (S7)


oscillate:
or s4, s7, r0
or v0, r0, s6
sin:
sw v0, 0x10 (SP)
mtc1 V0, f12 //VI timer
lui at, 0x3D20
cvt.s.w f12, f12
mtc1 at, f20
jal 0x80325480 //takes f12 returns f0
mul.s f12, f12, f20 //VI timer*modulation
lui t9, 0x42ff //127.5
mtc1 t9, f2
mul.s f4, f0, f2 //sin times 128
add.s f4, f2, f4 //sin oscillating 0-255
cvt.w.s f4, f4
mfc1 t1, f4
andi t2, s4, 0xff
sb t1, 0x6d3C (S4)
lw v0, 0x10 (SP)
addiu v0, v0, 0x78
addiu s4, s4, 0x1
li t3, 0x2
bne t3, t2, sin
lw t8, 0x18 (SP)
lbu t7, 0x1 (T8) //alpha value
sb t7, 0x6d3F (S7)
lw t6, 0x6d3C (S7)
sw t6, 0x6d38 (S7)

jal 0x80404e00 //set env color
nop

//set stuff up for next string
endoscillate:
jal 0x80405b0c //update x pos of string
nop
jal 0x80405118 //pop matrix
nop
jal 0x80405138 //push scale matrix
nop
jal 0x804051c0 //fix translation after scale
nop

lw t9, 0x6d0c (S7) //temp string
lw t1, 0x6df0 (S7) //current pos in string

lw t2, 0x6d14 (S7)
subu t2, t2, t1
sw t2, 0x6d14 (S7)
sw r0, 0x6df0 (S7)
sw r0, 0x6d54 (S7)
addu t9, t1, t9 //last byte read
addiu t9, t9, 0x2
beq r0, r0, parse
sw t9, 0x6d0c (S7)

//47 cmd
translateorigin:
jal 0x80404fb0 //loads unaligned half, t8 in, t4 out
lw at, 0x6d0c (S7)
or t3, r0, t4
jal 0x80404fb0
addiu t8, t8, 2
sh t3, 0x6df4 (S7)
sh t4, 0x6df6 (S7)
addiu at, at, 0x5
beq r0, r0, parse
sw at, 0x6d0c (s7)

//48
jumptext:
andi t9, t8, 0x3 //unaligned address mod 4
addiu t9, t9, 0x1
sll t9, t9, 3
subiu at, r0, 0x1
srlv t1, at, t9
sllv at, at, t9
LWR t7, 0x4 (T8)
and t7, t7, t1
LWL t8, 0x1 (T8)
and t8, t8, at
or a0, t7, t8 //a0 is word
beq r0, r0, parse
sw a0, 0x6d0c (S7)


//4C cmd
speeduptext:
lui t9, 0x8034
lhu t9, 0xafa0 (T9)
li t7, 0x8000
BNE t7, t9, nospeedup
lw at, 0x6d0c (S7)
lh t1, 0x6d08 (S7)
jal 0x80404fb0 //loads unaligned half, t8 in, t4 out
sh t1, 0x6d6C (S7)
sh t4, 0x6d08 (S7)
nospeedup:
addiu at, at, 3
beq r0, r0, parse
sw at, 0x6d0c (S7)


//4D cmd
disablefasttext:
lh t1, 0x6d6c (S7)
beq r0, r0, advancedbyone
sh t1, 0x6d08 (S7)


//84 cmd
scaletext:
jal 0x80404e00 //set env color
sw t8, 0x18 (SP)
lh a0, 0x6d10 (S7)
lh a1, 0x6d12 (S7)
jal 0x802d77dc //print string
ori a2, s7, 0x7000
jal 0x80404dd0 //flushstring
nop

//by popping the matrix, all previous scalings are gone
jal 0x80405118 //pop matrix
nop

//update positions

jal 0x80405b0c //update x pos of string
nop

//load new scales

lw t8, 0x18 (SP)
andi t9, t8, 0x3 //unaligned address mod 4
addiu t9, t9, 0x1
sll t9, t9, 3
nor t7, r0, t9
addiu t7, t7, 0x21
subiu at, r0, 0x1
srlv t1, at, t7
sllv at, at, t9
LWR t7, 0x4 (T8)
and t7, t7, t1
LWL t6, 0x1 (T8)
and t6, t6, at
or a1, t7, t6 //x scale
sw a1, 0x6d30 (S7)
LWR t7, 0x8 (T8)
and t7, t7, t1
LWL t6, 0x5 (T8)
and t6, t6, at
or a2, t7, t6 //y scale
sw a2, 0x6d34 (S7)

jal 0x80405138 //push scale matrix
nop
jal 0x804051c0 //fix translation after scale
nop
lw t9, 0x6d0c (S7) //temp string
lw t1, 0x6df0 (S7) //current pos in string
lw t2, 0x6d14 (S7)
subu t2, t2, t1
sw t2, 0x6d14 (S7) //temp string end pos
sw r0, 0x6df0 (S7)
sw r0, 0x6d54 (S7)
addu t9, t1, t9 //last byte read
addiu t9, t9, 0x9
beq r0, r0, parse
sw t9, 0x6d0c (S7)




//7F cmd
drawshadebox:
//make ortho matrix (don't need to call again after first one for text)
//pop mtx (to remove any previous scaling)
//env color
//scale push
//translate mult
//06 00 00 00 02 01 1c 48 (draws triangle
//pop matrix
//rescale+retranslate (so putting a box in the middle of a scaled string doesn't destroy it)


//draw previous string

jal 0x80404e00 //set env color
sw t8, 0x18 (SP)
lh a0, 0x6d10 (S7)
lh a1, 0x6d12 (S7)
jal 0x802d77dc //print string
ori a2, s7, 0x7000
jal 0x80404dd0 //flushstring
nop

//pop mtx to remove any scaling on previous string
jal 0x80405118 //pop matrix
nop

//push scale matrix, push translation matrix

//W=xf-xi
//H=yf-yi
//Sx=W/7=(xf-xi)/7
//Sy=H/10=(yf-yi)/10

//Tx=xi/Sx=7*xi/(xf-xi)
//Ty=yi/Sy=10*yi/(yf-yi)



jal 0x80404fb0 //load unaligned half, t8 in, t4 out
lw t8, 0x18 (SP)
or t1, r0, t4 //xi
jal 0x80404fb0 //load unaligned half, t8 in, t4 out
addiu t8, t8, 0x2
or t2, r0, t4 //xf
jal 0x80404fb0 //load unaligned half, t8 in, t4 out
addiu t8, t8, 0x2
or t3, r0, t4 //yi
jal 0x80404fb0 //load unaligned half, t8 in, t4 out
addiu t8, t8, 0x2

//t4 is yf

subu t2, t2, t1 //W
mtc1 t2, f2
subu t4, t4, t3 //H
cvt.s.w f2, f2 //Width
mtc1 t4, f4
lui at, 0x40e0 //7
cvt.s.w f4, f4 //H
mtc1 at, f10 //7
lui at, 0x4120 //10
mtc1 at, f20 //10
div.s f22, f2, f10 //Sx
div.s f14, f4, f20 //Sy
mtc1 t1, f12 //xi
mtc1 t3, f16 //yi
cvt.s.w f12, f12
div.s f12, f12, f22 //Tx
cvt.s.w f16, f16
div.s f16, f16, f14 //Ty
swc1 f16, 0x1C (SP) //Ty
mfc1 a1, f22 //Sx
swc1 f12, 0x20 (SP) //Tx
mfc1 a2, f14 //Sy
lui a3, 0x3f80 //1 = scale
jal 0x802d7280 //scale matrix
ori a0, r0, 0x1 //push matrix
lw a1, 0x20 (SP)
lw a2, 0x1C (SP)
or a3, r0, r0
jal 0x802d7070 //translate matrix
ori a0, r0, 0x2 //push matrix

//store env color, make new set combine and draw the rectangle

lw t8, 0x18 (SP)
addiu t8, t8, 0x8
andi t9, t8, 0x3 //unaligned address mod 4
addiu t9, t9, 0x1
sll t9, t9, 3
nor t7, r0, t9
addiu t7, t7, 0x21
subiu at, r0, 0x1
srlv t1, at, t7
sllv at, at, t9
LWR t7, 0x4 (T8)
and t7, t7, t1
LWL t6, 0x1 (T8)
and t6, t6, at
or t9, t7, t6 //env color
lui at, 0x8034
lw v0, 0xb06c (AT)
li t1, 0xFC62EAC5 
sw t1, 0x0 (V0)
li t2, 0xFFD7FFFF
sw t2, 0x4 (V0)
lui t6, 0xFB00
sw t6, 0x8 (V0)
sw t9, 0xC (V0)
lui t9, 0x0600
sw t9, 0x10 (V0)
li t7, 0x02011d08 //char triangle
sw t7, 0x14 (V0)
lui t1, 0xbd00 //pop mtx
sw t1, 0x18 (V0)
sw r0, 0x1C (V0)
addiu v0, v0, 0x20
sw v0, 0xb06c (AT)


//setup stuff for the next string

jal 0x80404d68 //init string
nop


jal 0x80405b0c //update x pos of string
nop
jal 0x80405118 //pop matrix
nop
jal 0x80405118 //pop matrix
nop
jal 0x80405138 //push scale matrix
nop
jal 0x804051c0 //fix translation after scale
nop


lw t9, 0x6d0c (S7) //temp string
lw t1, 0x6df0 (S7) //current pos in string
lw t2, 0x6d14 (S7)
subu t2, t2, t1
sw t2, 0x6d14 (S7) //temp string end pos
sw r0, 0x6df0 (S7)
sw r0, 0x6d54 (S7)
addu t9, t1, t9 //last byte read
addiu t9, t9, 0xD
beq r0, r0, parse
sw t9, 0x6d0c (S7)


//4F
userinputshortstring:
//80404ffC = lowercase
//8040505C = uppercase

//set combine for selected text is FC12EA25FFD7FFFF
//2A is the max value for the selector


lw at, 0x6d44 (S7) //flag for user input
BNE at, r0, makeuserinput
li t6, 0x1
sw r0, 0x6d50 (S7)
sh t6, 0x6d46 (S7)
sw t8, 0x6d4C (S7)
lbu t7, 0x1 (T8)
li t9, 0x10
multu t9, t7
mflo t9
addu t9, t9, s7
addiu t9, t9, 0x6f00
li t4, 0x9f9f9f9f
sw t4, 0x0 (T9)
li t3, 0x9f9f9fFF
sw t4, 0x4 (T9)
sw t4, 0x8 (T9)
sw t3, 0xC (T9)
beq r0, r0, printnone
sb T7, 0x6d44 (S7)



makeuserinput:

//draw background boxes for user input and keyboard
lui a1, 0x41F8
lui a2, 0x40E0
lui a3, 0x3f80 //1 = scale
jal 0x802d7280 //scale matrix
ori a0, r0, 0x1 //push matrix

lui a1, 0x3f80
or a2, r0, r0
or a3, r0, r0
jal 0x802d7070 //translate matrix
ori a0, r0, 0x2 //push matrix

lui at, 0x8034
lw v0, 0xb06c (AT)
li t1, 0xFC62EAC5 
sw t1, 0x0 (V0)
li t2, 0xFFD7FFFF
sw t2, 0x4 (V0)
lui t6, 0xFB00
sw t6, 0x8 (V0)
li t9, 0x60606060
sw t9, 0xC (V0)
lui t9, 0x0600
sw t9, 0x10 (V0)
li t7, 0x02011d08 //char triangle
sw t7, 0x14 (V0)
addiu v0, v0, 0x18
sw v0, 0xb06c (AT)


lui a1, 0x3f80
lui a2, 0x3EC0
lui a3, 0x3f80 //1 = scale
jal 0x802d7280 //scale matrix
ori a0, r0, 0x2 //mult matrix


or a1, r0, r0
lui a2, 0x4250
or a3, r0, r0
jal 0x802d7070 //translate matrix
ori a0, r0, 0x2 //push matrix

lui at, 0x8034
lw v0, 0xb06c (AT)
lui t9, 0x0600
sw t9, 0x0 (V0)
li t7, 0x02011d08 //char triangle
sw t7, 0x4 (V0)
addiu v0, v0, 0x8
sw v0, 0xb06c (AT)

jal 0x80405118 //pop matrix
nop

jal 0x80404d68 //init string
nop

jal 0x80404dd0 //flushstring
nop

lw at, 0x6d48 (S7)
beq at, s6, makekeyboarddisplay

li t0, 6
andi t2, s6, 0x6
bne t2, t0, beginkeyboard


//control selected text with control stick
li a0, 0x2 //horizontal
ori a1, s7, 0x6d47 //*menuvalue
li a2, 0x0 //min
jal 0x802d862c
li a3, 0x2b//max value

li a0, 0x1 //vert
ori a1, s7, 0x6d46 //*menuvalue
li a2, -1 //min
jal 0x802d862c
li a3, 0x1//max value

lb t0, 0x6d46 (S7)
lbu t1, 0x6d47 (S7)
li t2, 0xa
multu t2, t0
mflo t0
addu t0, t0, t1
sltiu t3, t0, 0x2c
bne t3, r0, capcontrolstickvert
nop
li t0, 0x2b
capcontrolstickvert:
sh t0, 0x6d46 (S7)


//control currently selected text with d pad

lh at, 0x6d46 (S7) //currently selected text
lui t0, 0x8034
lh t0, 0xafa0 (T0) //new btns
li t9, 0x0200 // D Left
bne t9, t0, checkDright
subiu t1, at, 0x1
beq r0, r0, beginkeyboard
sh t1, 0x6d46 (S7)
checkDright:
li t9, 0x0100
bne t9, t0, checkDup
addiu t1, at, 0x1
beq r0, r0, beginkeyboard
sh t1, 0x6d46 (S7)

checkDup:
li t9, 0x800 //Dup
bne t9, t0, checkDdown
subiu t1, at, 0x0a
beq r0, r0, beginkeyboard
sh t1, 0x6d46 (S7)
checkDdown:
li t9, 0x0400 //Ddown
bne t9, t0, beginkeyboard
sltiu t0, at, 0x21
beq t0, r0, ddowntospace
addiu t1, at, 0x0a
beq r0, r0, beginkeyboard
sh t1, 0x6d46 (S7)

ddowntospace:
li t1, 0x2a
sh t1, 0x6d46 (S7)

beginkeyboard:
//check for overflow or underflow on selected
lh t9, 0x6d46 (S7) //currently selected text
subiu t0, t9, 0x1
bltz t0, keyboardunderflow
sltiu at, t9, 0x2C
beq at, r0, keyboardoverflow
nop
beq r0, r0, makekeyboarddisplay
keyboardoverflow:
li t8, 0x1
beq r0, r0, makekeyboarddisplay
sh t8, 0x6d46 (S7)
keyboardunderflow:
li t7, 0x2B
sh t7, 0x6d46 (S7)



//80404ffC = lowercase
//8040505C = uppercase

makekeyboarddisplay:

lui a1, 0x4000
lui a2, 0x3f9A
lui a3, 0x3f80 //1 = scale
jal 0x802d7280 //scale matrix
ori a0, r0, 0x1 //push matrix

lh t9, 0x6d46 (S7) //currently selected text
lb t7, 0x6d45 (S7) //shift selected or not
lui at, 0x8040
beq t7, r0, lowercase
ori s5, at, 0x4FFC //lowercaseset
ori s5, at, 0x505C //uppercaseset
lowercase:
or t5, r0, r0
or t8, r0, s7
or s2, r0, r0//letter spacing counter
or s3, r0, r0 //line break counter
loopkeyboard1:
li t2, 0x2B
beql t2, t9, noaccountforspace
addiu t9, t9, 0x4
noaccountforspace:
lbu t1, 0x0 (S5)
li t2, 0x9e //space
beq t2, t1, nocountforselector1
li t3, 0xfe
bne t3, t1, countforselector1
nop
subiu s2, r0, 0x1
beq r0, r0, nocountforselector1
subiu s3, s3, 0x10

countforselector1:
addiu t5, t5, 0x1

nocountforselector1:
li t3, 0xff
sb t3, 0x7001 (T8)
beq t3, t1, breakkeyboardloop1
addiu t8, t8, 0x1
beq t5, t9, breakkeyboardloop1
nop
addiu s2, s2, 0x1
addiu s5, s5, 0x1
beq r0, r0, loopkeyboard1
sb t1, 0x7000 (t8)
breakkeyboardloop1:



li a0, 0x12
li a1, 0x46
lui a2, 0x807f
jal 0x802d77dc //print string
ori a2, A2, 0x7000

jal 0x80404dd0 //flushstring
nop

//change setcombine so that black background goes around current letter
//2A = space
//2B = end


li t1, 0xFC12EA25
li t0, 0xFFD7FFFF
lui at, 0x8034
lw v0, 0xb06c (AT)
sw t1, 0x0 (V0)
sw t0, 0x4 (V0)
addiu v0, v0, 0x8
sw v0, 0xb06c (AT)

//print single letter with black background

li t6, 0
lui t8, 0x807f
lh t9, 0x6d46 (S7)
li at, 0x2b
beq at, t9, endletter
li t0, 0x2a
bne t0, t9, singleletter
nop
addiu s2, s2, 0x1
space:
li t3, 0xff
lbu t1, 0x0 (S5)
addiu S5, S5, 0x1
addiu t6, t6, 0x1
sb t3, 0x7001 (T8)
li t0, 0x5
sb t1, 0x7000 (T8)
bne t0, t6, space
addiu t8, t8, 0x1
beq r0, r0, printhighlightedletter
nop

endletter:
li t3, 0xff
lbu t1, 0x0 (S5)
addiu S5, S5, 0x1
addiu t6, t6, 0x1
sb t3, 0x7001 (T8)
li t0, 0x3
sb t1, 0x7000 (T8)
bne t0, t6, endletter
addiu t8, t8, 0x1
beq r0, r0, printhighlightedletter



singleletter:
li t3, 0xff
lbu t1, 0x0 (S5)
addiu S5, S5, 0x1
sb t3, 0x7001 (S7)
sb t1, 0x7000 (S7)



printhighlightedletter:
mtc1 s2, f2
cvt.s.w f2, f2
lh t9, 0x6d46 (S7)
sltiu t9, t9, 0xb
bne t9, r0, smallerkerning1
lui at, 0x40C8
lb t0, 0x6d45 (S7)
beq t0, r0, smallerkerning1
lui at, 0x40B0
lui at, 0x40C8
smallerkerning1:
mtc1 at, f4
mul.s f2, f2, f4
cvt.w.s f2, f2
mfc1 t0, f2


li a0, 0x12
addu a0, a0, t0
li a1, 0x46
addu a1, a1, s3
lui a2, 0x807f
jal 0x802d77dc //print string
ori a2, A2, 0x7000

jal 0x80404dd0 //flushstring
nop

jal 0x80404d68 //init string
addiu s2, s2, 0x1


lui a1, 0x4000
lui a2, 0x3f9A
lui a3, 0x3f80 //1 = scale
jal 0x802d7280 //scale matrix
ori a0, r0, 0x1 //push matrix



lui t8, 0x807f
loopkeyboard2:
lbu t1, 0x0 (S5)
li t3, 0xfe
beq t3, t1, breakkeyboardloop2
li t2, 0xff
sb t1, 0x7000 (t8)
beq t2, t1, breakkeyboardloop3
sb t2, 0x7001 (T8)
addiu t8, t8, 0x1
bne t5, t9, loopkeyboard2
addiu s5, s5, 0x1
breakkeyboardloop2:



mtc1 s2, f2
cvt.s.w f2, f2
lh t9, 0x6d46 (S7)
sltiu t9, t9, 0xb
bne t9, r0, smallerkerning2
lui at, 0x40C8
lb t0, 0x6d45 (S7)
beq t0, r0, smallerkerning2
lui at, 0x40B0
lui at, 0x40C8
smallerkerning2:
mtc1 at, f4
mul.s f2, f2, f4
cvt.w.s f2, f2
mfc1 t0, f2



li a0, 0x12
addu a0, a0, t0
li a1, 0x46
addu a1, a1, s3
lui a2, 0x807f
jal 0x802d77dc //print string
ori a2, A2, 0x7000



lui t8, 0x807f
loopkeyboard3:
lbu t1, 0x0 (S5)
sb t1, 0x7000 (t8)
li t3, 0xff
sb t3, 0x7001 (T8)
beq t3, t1, breakkeyboardloop3
addiu t8, t8, 0x1
bne t5, t9, loopkeyboard3
addiu s5, s5, 0x1
breakkeyboardloop3:


lh t9, 0x6d46 (S7)
li t7, 0x29
bne t7, t9, checkspacebarspace
li a0, 0x12
beq r0, r0, printfinalkeyboard
addiu a0, a0, 0x18
checkspacebarspace:
li t8, 0x2a
bne t8, t9, printfinalkeyboard
li a0, 0x12
addiu a0, a0, 0x46

printfinalkeyboard:
li a1, 0x46
addu a1, a1, s3
lui a2, 0x807f
jal 0x802d77dc //print string
ori a2, A2, 0x7000



//keyboard has been properly displayed, now I will print the string I am writing above

lb T7, 0x6d44 (S7)
li t9, 0x10
multu t7, t9
mflo t9
addu t9, t9, s7
addiu t9, t9, 0x6f00 //address of string


//display user string

li a0, 0x18
li a1, 0x76
jal 0x802d77dc //print string
or a2, r0, T9

jal 0x80405118 //pop matrix
nop

//add to string on A press
lw at, 0x6d48 (S7)
beq at, s6, printnone
nop
sw s6, 0x6d48 (S7)
lbu t7, 0x6d44 (S7)
li t6, 0x10
multu t6, t7
mflo t6
addu t6, t6, s7
addiu t6, t6, 0x6f00
lw t8, 0x6d50 (S7) //spot in user string
addu t6, t8, t6
lh t9, 0x6d46 (S7)
lui t0, 0x8034
lh t0, 0xafa2 (T0)
andi t2, t0, 0x4000
bne t2, r0, backspace
andi t1, t0, 0x8000
beq t1, r0, printnone
li at, 0x2b
beq at, t9, endkeyboard
li t1, 0x29
beq t1, t9, shift
li at, 0x2a
beq at, t9, addspace
li t1, 0x28
beq t1, t9, backspace
li at, 0xF
beq at, t8, printnone



//80404ffC = lowercase
//8040505C = uppercase

//every other letter
lb t7, 0x6d45 (S7) //shift selected or not
ori t0, r0, 0x1
lui at, 0x8040
beq t7, r0, lowercaseinput
ori s5, at, 0x4ffC //lowercaseset
ori s5, at, 0x505C //uppercaseset


lowercaseinput:
lbu t4, 0x0 (S5)
addiu s5, s5, 0x1
li t3, 0x9e
beq t3, t4, lowercaseinput
li t2, 0xfe
beq t2, t4, lowercaseinput
nop
beq t0, t9, addletterinput
nop
beq r0, r0, lowercaseinput
addiu t0, t0, 0x1


addletterinput:
sb t4, 0x0 (T6)
addiu t8, t8, 0x1
beq r0, r0, printnone
sw t8, 0x6d50 (S7)


backspace:
blez t8, printnone
subiu t6, t6, 0x1
li t2, 0x9f
sb t2, 0x0 (T6)
subiu t8, t8, 0x1
beq r0, r0, printnone
sw t8, 0x6d50 (S7)



shift:
lb t2, 0x6d45 (S7)
xori t2, t2, 0x1
beq r0, r0, printnone
sb t2, 0x6d45 (S7)


addspace:
li at, 0xF
beq at, t8, printnone
li t2, 0x9e
sb t2, 0x0 (T6)
addiu t8, t8, 0x1
beq r0, r0, printnone
sw t8, 0x6d50 (S7)



//end keyboard, save input

endkeyboard:
li t2, 0x45
sb t2, 0x0 (T6)
lw t8, 0x6d4C (S7)
sw r0, 0x6d50 (S7)
sw r0, 0x6d4C (S7)
sw r0, 0x6d44 (S7)
sw r0, 0x6d48 (S7)
lw at, 0x6d0c (S7) //temp string
addiu at, at, 0x2
sw at, 0x6d0c (S7) //temp string

lh at, 0x6d0a (S7)
beq at, r0, paddoutkeyboard

//a0 = location start
//a1 = num of bytes to add to buffer
or a0, r0, t8
jal 0x80404ea8
li A1, 2

beq r0, r0, parse

paddoutkeyboard:
li t9, 0x9d
sb t9, 0x0 (T8)
sb t9, 0x1 (T8)
beq r0, r0, parse
sh r0, 0x6d0a (S7)








//9D cmd
blankout:
beq r0, r0, advancedbyone




//72 cmd
nodisplayonVI:
lw at, 0x6d24 (S7) //VI timer for no display
BNE at, r0, checkVItimefordisplay
nop
sw s6, 0x6d24 (S7)
beq r0, r0, printnone
sw t8, 0x6d40 (S7)
checkVItimefordisplay:
jal 0x80404fb0 //load unaligned half, t8 in, t4 out
lw t8, 0x6d40 (S7)
addu at, t4, at //VI no display should end at
subu at, s6, at
bltz at, printnone
lw t1, 0x6d0c (S7) //temp string
addiu t4, t1, 0x3
sw r0, 0x6d24 (S7)

//a0 = location start
//a1 = num of bytes to add to buffer
or a0, r0, t8
jal 0x80404ea8
li A1, 3

beq r0, r0, parse
sw t4, 0x6d0c (S7) //temp string


//4E cmd
enablereinput:
li t9, 1
beq r0, r0, advancedbyone
sh t9, 0x6d0a (S7)



//74 cmd
pauseforVI:
lw at, 0x6d2C (S7)
BEQL at, r0, printnone
sw s6, 0x6d2C (S7)
jal 0x80404fb0 //load unaligned half, t8 in, t4 out
nop
addu at, t4, at //VI that the pause should end at
subu at, s6, at
bltz at, nonewchar
lw t1, 0x6d0c (S7) //temp string
addiu t4, t1, 0x3
sw r0, 0x6d2c (S7)

//a0 = location start
//a1 = num of bytes to add to buffer
or a0, r0, t8
jal 0x80404ea8
li A1, 3

beq r0, r0, parse
sw t4, 0x6d0c (S7) //temp string



//76 cmd
enableblip:
lh t9, 0x6d1A (S7)
ori t9, t9, 0x1
beq r0, r0, advancedbyone
sh t9, 0x6d1A (S7)



//77 cmd
disableblip:
beq r0, r0, advancedbyone
sh r0, 0x6d1A (S7)



//78 cmd
changeoriginalmusic:
lh t2, 0x6d22 (S7) //switched to music
beq r0, r0, advancedbyone
sh t2, 0x6d20 (S7) //original music


//79 cmd
playmusictrack:
lui at, 0x8034
lh t1, 0xb944 (AT) //currently playing music
lh t2, 0x6d20 (S7) //original music
bltzl t2, skipsetogmusic
sh t1, 0x6d20 (S7)
skipsetogmusic:
lbu a1, 0x1 (T8) //music we want to play
lh t3, 0x6d22 (S7)
beq t3, a1, skipenablemusic
or a0, r0, r0
sh a1, 0xb944 (AT) //currently playing
jal 0x80320544 //set music
sh a1, 0x6d22 (S7) //switched to music
skipenablemusic:
lw at, 0x6d0c (S7) //temp string
addiu at, at, 0x2
beq r0, r0, parse
sw at, 0x6d0c (S7) //temp string





//7B cmd
removestringonVI:
lw at, 0x6d28 (S7)
BEQL at, r0, nocheckVIforstringend
sw s6, 0x6d28 (S7)
jal 0x80404fb0 //load unaligned half, t8 in, t4 out
nop
addu at, t4, at //VI string should end at
subu at, s6, at
blez at, nocheckVIforstringend
nop
jal 0x80405160 //resetstring
sw r0, 0x6d2c (S7)
beq r0, r0, end
sw r0, 0x6d24 (S7)
nocheckVIforstringend:
li t7, 0xff
lw t9, 0x6df0 (S7) //current pos in string
addu t9, t9, s7
beq r0, r0, nonewchar
sb t7, 0x7000 (T9)



//7C cmd
removestringonA:
lui t9, 0x8034
lh t9, 0xafa2 (T9) //new buttons
andi t0, t9, 0x8000 //a press
//flash down arrow
andi t1, s6, 0x20
beq r0, t1, noarrowforend7c
li t7, 0xff
li t7, 0x51
noarrowforend7c:
li t6, 0xff
lw t9, 0x6df0 (S7) //current pos in string
addu t9, t9, s7
sb t6, 0x7001 (T9)
beq t0, r0, nonewchar
sb t7, 0x7000 (T9)
jal 0x80405160 //resetstring
nop
beq r0, r0, end
nop


//85 cmd
startdialogoptions:

//print string before dialog options

jal 0x80404e00 //set env color
sw t8, 0x18 (SP)
lh a0, 0x6d10 (S7)
lh a1, 0x6d12 (S7)
jal 0x802d77dc //print string
ori a2, s7, 0x7000
jal 0x80404dd0 //flushstring
nop
lh a0, 0x6df4 (S7)
jal 0x80405118 //pop matrix
sh a0, 0x6d10 (S7)
jal 0x80405138 //push scale matrix
nop
jal 0x804051c0 //fix translation after scale
nop

//6d58 will store returned dialog option
//6d59 will store currently displaying dialog option
//6d5A will store total number of options
//6d5B will store hovered dialog option


//line break for first option to be under question dialog
lh a1, 0x6d12 (S7) //temp y pos
subiu a1, a1, 0xb
sh a1, 0x6d12 (S7)

//store total num of options
lw t8, 0x18 (SP)
lbu t0, 0x1 (T8) //number of options
sb t0, 0x6d5a (S7)

//check for dialog selection
lw t0, 0x6d5C (S7)
beq t0, r0, setmenuvaluesdialog
lui t9, 0x8034
lh t9, 0xafa2 (T9)
andi t9, t9, 0x8000
beq t9, r0, setmenuvaluesdialog
lbu t0, 0x6d5b (S7)
sb t0, 0x6d58 (S7)
sh r0, 0x6d5A (S7)
sb r0, 0x6d59 (S7)
lw t9, 0x6d5C (S7)
sw r0, 0x6df0 (S7)
sw r0, 0x6d54 (S7)
sw r0, 0x6dfc (S7)
beq r0, r0, printnone
sw t9, 0x6df8 (S7) //*string



setmenuvaluesdialog:
//update end of string ptr
sb r0, 0x6d59 (S7)
lw t1, 0x6df0 (S7)
lw t0, 0x6d14 (S7)
subu t0, t0, t1
sw t0, 0x6d14 (S7)




//get control stick input for menu selection
li a0, 0x1 //vert
ori a1, s7, 0x6d5B //*menuvalue
li a2, 0 //min
jal 0x802d862c
lbu a3, 0x6d5a (S7) //max value

//draw arrow if option 0 is selected

lb t0, 0x6d5b (S7)
bne t0, r0, nosetarrowdialog
li t1, 0x53
li at, 0xff
sb at, 0x7001 (S7)
jal 0x80404e00 //set env color
sb t1, 0x7000 (S7)
lh a0, 0x6df4 (S7)
lh a1, 0x6d12 (S7) //temp y pos
jal 0x802d77dc //print string
ori a2, s7, 0x7000
jal 0x80404dd0 //flushstring
nop
jal 0x80405118 //pop matrix
nop
jal 0x80405138 //push scale matrix
nop
jal 0x804051c0 //fix translation after scale
nop

nosetarrowdialog:

//set x pos to be init pos for line break
lh a0, 0x6df4 (S7)
sh a0, 0x6d10 (S7)
li t0, 0x1
sw r0, 0x6d54 (S7)
jal 0x80405b0c //update x pos of string
sw t0, 0x6dF0 (S7)
sw r0, 0x6dF0 (S7)
lw at, 0x18 (SP) //*temp string
addiu at, at, 0x2
beq r0, r0, parse
sw at, 0x6d0c (S7) //*temp string 



//86 cmd
displayoncorrectdialogreturn:
lbu t1, 0x1 (T8)
lbu t0, 0x6d58 (S7)
lw t2, 0x6d0c (S7)
addiu t2, t2, 0x2
beq t1, t0, parse
sw t2, 0x6d0c (S7)

notcorrectoption:
addiu t8, t8, 0x1
lbu t0, 0x0 (T8)
li t2, 0x87
beql t0, t2, generaltextdisplay
addiu t7, t8, 1
li t3, 0x86
bne t0, t3, notcorrectoption
or t7, t8, r0
generaltextdisplay:
jal 0x80404e00 //set env color
sw t7, 0x18 (SP)
lh a0, 0x6d10 (S7)
lh a1, 0x6d12 (S7)
jal 0x802d77dc //print string
ori a2, s7, 0x7000
jal 0x80404dd0 //flushstring
lw v1, 0x6df0 (S7) //current pos in string
jal 0x80405118 //pop matrix
lw gp, 0x6d14 (S7)
jal 0x80405138 //push scale matrix
subu gp, gp, v1
jal 0x804051c0 //fix translation after scale
sw gp, 0x6d14 (S7)
jal 0x80405b0c //update x pos of string
lw gp, 0x18 (SP)
sw r0, 0x6df0 (S7)
beq r0, r0, parse
sw gp, 0x6d0c (S7)

//88 cmd
shakescreen:
li at, 1
sb at, 0x6d60 (s7)
advancedbyone:
lw t0, 0x6d0c (S7)
addiu t0, t0, 0x1
beq r0, r0, parse
sw t0, 0x6d0c (S7)

//89 cmd
disablescreenshake:
beq r0, r0, advancedbyone
sb r0, 0x6d60 (s7)


//44 cmd
longuserstring:
lw at, 0x6d0c (S7) //temp string
addiu at, at, 0x1
sw at, 0x6d1c (S7) //*string return loc
ori t1, s7, 0x6e00
beq r0, r0, parse
sw t1, 0x6d0c (S7)


//43 cmd
shortuserstring:
jal 0x80404e00 //set env color
sw t8, 0x18 (SP)
lh a0, 0x6d10 (S7)
lh a1, 0x6d12 (S7)
jal 0x802d77dc //print string
ori a2, s7, 0x7000
jal 0x80404dd0 //flushstring
nop
jal 0x80405b0c //update x pos of string
nop
jal 0x80405118 //pop matrix
nop
jal 0x80405138 //push scale matrix
nop
jal 0x804051c0 //fix translation after scale
nop
lw t1, 0x6df0 (S7) //current pos in string
lw t2, 0x6d14 (S7)
subu t2, t2, t1
sw t2, 0x6d14 (S7)
sw r0, 0x6df0 (S7)
sw r0, 0x6d54 (S7)
lw t8, 0x18 (SP)
lw at, 0x6d0c (S7) //temp string
addu at, at, t1
addiu at, at, 0x2
sw at, 0x6d1c (S7) //*string return loc
ori t1, s7, 0x6f00
lbu t9, 0x1 (T8)
li at, 0x10
multu t9, at
mflo at
addu t1, t1, at
beq r0, r0, parse
sw t1, 0x6d0c (S7)


//45 cmd
returnuserstring:
jal 0x80404e00 //set env color
sw t8, 0x18 (SP)
lh a0, 0x6d10 (S7)
lh a1, 0x6d12 (S7)
jal 0x802d77dc //print string
ori a2, s7, 0x7000
jal 0x80404dd0 //flushstring
nop

jal 0x80405b0c //update x pos of string
nop
jal 0x80405118 //pop matrix
nop
jal 0x80405138 //push scale matrix
nop
jal 0x804051c0 //fix translation after scale
nop

lw t1, 0x6df0 (S7) //current pos in string

lw t2, 0x6d14 (S7)
subu t2, t2, t1
sw t2, 0x6d14 (S7) //temp end pos
sw r0, 0x6df0 (S7)
sw r0, 0x6d54 (S7)
lw at, 0x6d1c (S7) //temp string
beq r0, r0, parse
sw at, 0x6d0c (S7) //*string return loc


//41 cmd
setsfx:
lw at, 0x6d0c (S7) //temp string
addiu at, at, 0x3
sw at, 0x6d0c (S7)

lw t9, 0x6df0 (S7) //current pos in string
lw t6, 0x6d14 (S7) //end pos in string
subu t7, t6, t9
bne t7, r0, parse
nop
jal 0x80404fb0 //load unaligned half, t8 in, t4 out
nop
beq r0, r0, parse
sh t4, 0x6d18 (S7)



//40 cmd
setspeed:
jal 0x80404fb0 //load unaligned half, t8 in, t4 out
nop
sh t4, 0x6d08 (S7)
lw t9, 0x6d0c (S7) //temp string
addiu t9, t9, 0x3
beq r0, r0, parse
sw t9, 0x6d0c (S7)


//FE cmd (not really a cmd but I made it one)
linebreak:
jal 0x80404e00 //set env color
sw t8, 0x18 (SP)
li t0, 0x9eff
lh at, 0x7000 (S7)
beq at, t0, noprintLB
lh a0, 0x6d10 (S7)
lh a1, 0x6d12 (S7)
jal 0x802d77dc //print string
ori a2, s7, 0x7000
lh a0, 0x6df4 (S7) //initial x cord
sh a0, 0x6d10 (S7) //temp x pos
lh a1, 0x6d12 (S7) //temp y pos
subiu a1, a1, 0xb
jal 0x80404dd0 //flushstring
sh a1, 0x6d12 (S7)

//reset pos to zero so x pos doesn't get fuked, readjust string end pos to match
noprintLB:
lw at, 0x6df0 (S7) //current string pos
lw t1, 0x6d14 (S7) //temp string end pos
subu t1, t1, at
sw t1, 0x6d14 (S7)
sw r0, 0x6df0 (S7)
sw r0, 0x6d54 (S7)

lw t9, 0x6d0c (S7) //temp string pos
addu t9, t9, at
addiu t9, t9, 0x1

//reapply scaling and fix translation

jal 0x80405118 //pop matrix
sw t9, 0x6d0c (S7)
jal 0x80405138 //push scale matrix
nop
jal 0x804051c0 //fix translation after scale
nop

beq r0, r0, parse
nop



//70 cmd
AutoBoxEnd:
lw at, 0x6df0 (S7) //temp string pos
sw r0, 0x6df0 (S7)
lw t9, 0x6d0c (S7) //temp *string
addu t9, t9, at
addiu t9, t9, 0x1
sw t9, 0x6df8 (S7) //*string
li t7, 0xff
sb t7, 0x7000 (S7)
lh t1, 0x6d6c (S7)
sh t1, 0x6d08 (S7)
beq r0, r0, printnone
sw r0, 0x6dfc (S7) //end string pos


//71 cmd
ABoxEnd:
lui t9, 0x8034
lh t9, 0xafa2 (T9) //new buttons
andi t9, t9, 0x8000 //a press
andi t1, s6, 0x20
beq r0, t1, noarrowforend71
li t7, 0xff
li t7, 0x51
noarrowforend71:
li t6, 0xff
lw at, 0x6df0 (S7) //temp string pos
addu AT, S7, at
sb t6, 0x7001 (AT)
beq t9, r0, nonewchar
sb t7, 0x7000 (AT)
beq r0, r0, AutoBoxEnd
nop


//FF cmd (just stops display, does not remove string)
endofstring:

//draw previous string
jal 0x80404e00 //set env color
sw t8, 0x18 (SP)
lh a0, 0x6d10 (S7)
lh a1, 0x6d12 (S7)
jal 0x802d77dc //print string
ori a2, s7, 0x7000
jal 0x80404dd0 //flushstring
nop
jal 0x80405118 //pop matrix
nop
jal 0x80405138 //push scale matrix
nop
jal 0x804051c0 //fix translation after scale
nop

//update *end of string
lw t1, 0x6df0 (S7)
lw t0, 0x6d14 (S7)
subu t0, t0, t1
sw t0, 0x6d14 (S7)

//update *temp string 
lw v1, 0x6d0c (S7) //*temp string
addu v1, v1, t1
addiu v1, v1, 0x1
sw v1, 0x6d0c (S7) //*temp string 


//set x pos to be init pos for line break
sw r0, 0x6d54 (S7)
jal 0x80405b0c //update x pos of string
sw r0, 0x6df0 (S7) //temp x pos

//line break y pos
lh a1, 0x6d12 (S7) //temp y pos
subiu a1, a1, 0xb
sh a1, 0x6d12 (S7)


//6d58 will store returned dialog option
//6d59 will store currently displaying dialog option
//6d5A will store total number of options
//6d5B will store hovered dialog option

//end string after all options are drawn
lbu t0, 0x6d59 (S7)
lbu t1, 0x6d5A (S7)
BEQL t1, t0, printnone
sw v1, 0x6d5C (S7)
addiu t3, t0, 1
//draw arrow on current hovered line
sb t3, 0x6d59 (S7)
lb t2, 0x6d5b (S7)
bne t2, t3, noarrowenddialog
li t1, 0x53
li at, 0xff
sb at, 0x7001 (S7)


jal 0x80404e00 //set env color
sb t1, 0x7000 (S7)
lh a0, 0x6df4 (S7)
sh a0, 0x6d10 (S7)
lh a1, 0x6d12 (S7) //temp y pos
jal 0x802d77dc //print string
ori a2, s7, 0x7000
jal 0x80404dd0 //flushstring
li t9, 0x1
jal 0x80405b0c //update x pos of string
sw t9, 0x6df0 (S7)
jal 0x80405118 //pop matrix
nop
jal 0x80405138 //push scale matrix
nop
jal 0x804051c0 //fix translation after scale
nop

noarrowenddialog:
lw t1, 0x6d0c (S7)
sw r0, 0x6d54 (S7)
beq r0, r0, parse
sw r0, 0x6df0 (S7)




//the following is just a character not a cmd
normalchar:
lw t9, 0x6df0 (S7) //current pos in string
lw t8, 0x6d14 (S7) //end pos in string
subu t7, t8, t9
bne t7, r0, advancedchar

//here I check if I should advanced the end pos of the string, which should happen when a new chars time is here

lw at, 0x6d00 (S7) //VI of last character
lh t7, 0x6d08 (S7) //VIs per character
addu t8, t7, at //check if new char should be drawn
subu t8, s6, t8
//compares VI of last char drawn + wait time to current VI.
bltz t8, nonewchar

//here is a check to play a sfx
lh a0, 0x6d18 (S7) //sfx pointer
beq a0, r0, checkblip
sll a0, a0, 16
lui a1, 0x8033
ori a0, a0, 0x0081
jal 0x8031eb00
ori a1, a1, 0x31f0

checkblip:
lh a0, 0x6d1a (S7) //sfx check
beq a0, r0, drawnewchar
lui a1, 0x8033
lui a0, 0x16 //ice (placeholder)
ori a0, a0, 0xff81
jal 0x8031eb00
ori a1, a1, 0x31f0




drawnewchar:
lw t8, 0x6dfc (S7) //end pos in string
addiu t8, t8, 0x1
sw t8, 0x6dfc (S7)
lw t8, 0x6d14 (S7) //temp end pos in string
addiu t8, t8, 0x1
sw t8, 0x6d14 (S7)
sw s6, 0x6d00 (S7) //make current VI the VI of last char
advancedchar:
lw t9, 0x6df0 (S7) //current pos in string
addu at, s7, t9
lw t8, 0x6d0c (S7)
addu t8, t9, t8
lbu t7, 0x0 (T8) //char in string to write

//24 to 3D are lowercase letters
subiu t6, t7, 0x24
sltiu t6, t6, 0x1a
beq t6, r0, uppercaseincrement
lw t5, 0x6d54 (S7)
addiu t5, t5, 0x1
sw t5, 0x6d54 (S7)

uppercaseincrement:
sb t7, 0x7000 (AT)
li t6, 0xff
sb t6, 0x7001 (AT)
addiu t9, t9, 0x1
beq r0, r0, parse
sw t9, 0x6df0 (S7) //advanced position in string

//The string has gone as far as it needs to go
nonewchar:

//print whatever has not been printed already

jal 0x80404e00 //set env color
nop

lh a0, 0x6d10 (S7)
lh a1, 0x6d12 (S7)
jal 0x802d77dc
ori a2, s7, 0x7000

printnone:

lb at, 0x6d60 (S7)
beq at, r0, noscreenshake
nop
jal 0x802A50FC //shake screen
li a0, 0x1

noscreenshake:
//end a string
jal 0x80404da8 //end string
sh r0, 0x6d62 (S7)
sw v0, 0x6d64 (S7) //end ptr
jal 0x80404dd0 //flushstring
sw r0, 0x6df0 (S7)

end:
lw ra, 0x14 (SP)
jr ra
addiu sp, sp, 0x20



.orga 0x36A80
jal 0x80403800 //after shadows entry point that seems to work
nop



//80405218
//enable dialog with this object
//a0=x
//a1=y
//a2=*string
//everything but this object and mario will freeze, when dialog ends, everything will unfreeze (aka a 7C or a 7B cmd)

.orga 0x1205218
addiu sp, sp, -0x18
sw ra, 0x14 (SP)
lw t0, 0x80361160
lw t1, 0x90 (T0)
bne t1, r0, checkstringendobj
lui s7, 0x807f
lw at, 0x6df8 (S7)
bne at, r0, objsetupdone
li t1, 1
jal 0x80404fc4 //setup string
lh v1, 0x74 (T0)
ori v1, v1, 0x20
sh v1, 0x74 (T0)
lui at, 0x8034
lw t9, 0xd480 (AT)
ori t9, t9, 0x2
beq r0, r0, endobjsetup
sw t9, 0xd480 (AT)

objsetupdone:
beq r0, r0, endobjsetup
sw t1, 0x90 (T0)

checkstringendobj:
lw at, 0x6df8 (S7)
bne at, r0, endobjsetup
lh t6, 0x74 (T0)
xori t6, t6, 0x20
srl at, at, 8
sh t6, 0x74 (T0)
lui at, 0x8034
sw r0, 0xd480 (AT)
sw r0, 0x90 (T0)
lui t9, 0x8034
li t6, 0x0C400201 //idle
sw t6, 0xb17c (T9)

endobjsetup:
lw ra, 0x14 (SP)
jr ra
addiu sp, sp, 0x18



//sets up string
//80404fc4
//a0=x
//a1=y
//a2=*string

.orga 0x1204FC4
addiu sp, sp, -0x18
sw ra, 0x14 (SP)
lui s7, 0x807f
sh a0, 0x6df4 (S7)
sh a1, 0x6df6 (S7)
sw a2, 0x6d68 (S7)
jal 0x80404DD0 //flush string
sw a2, 0x6df8 (S7)
subiu t1, r0, 1
sh t1, 0x6d20 (S7)
sh t1, 0x6d22 (S7)
lw ra, 0x14 (SP)
jr ra
addiu sp, sp, 0x18



//80404fb0
.orga 0x1204FB0 //load unaligned half, t8 holds address, returns t4
Lbu t9, 0x1 (T8) //params of cmd
Lbu t4, 0x2 (T8) //params of cmd
sll t9, t9, 8
jr ra
or t4, t9, t4 //xi


//80405b0c
.orga 0x1205b0c //update x pos of string
lw t1, 0x6df0 (S7) //current pos in string
lw t2, 0x6d54 (S7) //num lowercase letters
mtc1 t2, f14 //num lowercase
subu t1, t1, t2 //num uppercase letters
mtc1 t1, f12 //uppercase count
lui t0, 0x40a0//5
cvt.s.w f12, f12
mtc1 t0, f10 //5
cvt.s.w f14, f14
lui t3, 0x40d0//6.5
mtc1 t3, f2 //6.5
mul.s f2, f2, f12 //uppercase*6.5
mul.s f10, f10, f14//lowercase*5
lwc1 f4, 0x6d30 (S7) //x scale
add.s f2, f10, f2
mul.s f2, f2, f4 //letters*xscale
cvt.w.s f2, f2
mfc1 t2, f2
lh a0, 0x6d10 (S7)
addu a0, a0, t2
sw r0, 0x6d54 (S7)
jr ra
sh a0, 0x6d10 (S7)


//80405118
.orga 0x1205118 //pop matrix
lui at, 0x8034
lw v0, 0xb06c (AT)
lui t6, 0xBD00
sw t6, 0x0 (V0)
sw r0, 0x4 (V0)
addiu v0, v0, 0x8
jr ra
sw v0, 0xb06c (AT)

//80405138
.orga 0x1205138 //scale matrix
addiu sp, sp, -0x18
sw ra, 0x14 (SP)

lw a1, 0x6d30 (S7)
lw a2, 0x6d34 (S7)
lui a3, 0x3f80 //1 = scale
jal 0x802d7280 //scale matrix
ori a0, r0, 0x1 //push matrix

lw ra, 0x14 (SP)
jr ra
addiu sp, sp, 0x18


//804051c0
.orga 0x12051c0 //fix string translate position
//fix positions for scale using this formula
//t=x/s-x=x(1/s-1)
addiu sp, sp, -0x18
sw ra, 0x14 (SP)
lwc1 f4, 0x6d30 (S7) //x scale
lwc1 f6, 0x6d34 (S7) //y scale
lui at, 0x3f80
mtc1 at, f20
lh a0, 0x6d10 (S7)
mtc1 a0, f0
lh t2, 0x6d12 (S7) //temp y
cvt.s.w f0, f0 //temp x
mtc1 t2, f2
div.s f4, f20, f4 //1/Sx
cvt.s.w f2, f2 //temp y
div.s f6, f20, f6 //1/Sy
sub.s f6, f6, f20 //1/Sy-1
sub.s f4, f4, f20 //1/Sx-1
mul.s f4, f4, f0 //X(1/Sx-1)
mul.s f6, f6, f2 //y(1/Sy-1)
mfc1 a1, f4 //x translate
mfc1 a2, f6 //y translate
or a3, r0, r0
jal 0x802d7070 //translate matrix
ori a0, r0, 0x2 //multiply
lw ra, 0x14 (SP)
jr ra
addiu sp, sp, 0x18



//insert char start ptr
//80404d68
.orga 0x1204D68
addiu sp, sp, -0x18
sw ra, 0x14 (SP)
jal 0x802d7384 //ortho matrix
nop
lui at, 0x8034
lw v0, 0xb06c (AT)
lui t6, 0x0600
sw t6, 0x0 (V0)
li t7, 0x02011cc8 //ia8 text string begin
sw t7, 0x4 (V0)
addiu v0, v0, 0x8
sw v0, 0xb06c (AT)
lw ra, 0x14 (SP)
jr ra
addiu sp, sp, 0x18



//insert char end ptr
//80404da8
.orga 0x1204DA8
lui at, 0x8034
lw v0, 0xb06c (AT)
lui t6, 0x0600
sw t6, 0x0 (V0)
li t7, 0x02011d50 //ia8 text end
sw t7, 0x4 (V0)
addiu v0, v0, 0x8
jr ra
sw v0, 0xb06c (AT)


//flush string
//80404dd0
.orga 0x1204DD0
lui at, 0x807f
flushnext:
lw t1, 0x7000 (AT)
beq t1, r0, endflush
sw r0, 0x7000 (AT)
beq r0, r0, flushnext
addiu at, at, 0x4
endflush:
li t1, 0xff
li t2, 0x9e
lui at, 0x807f
sb t2, 0x7000 (AT)
jr ra
sb t1, 0x7001 (AT)


.orga 0x1204E00 //set env color
//80404e00
lw a0, 0x6d38 (S7)
lui at, 0x8034
lw v0, 0xb06c (AT)
lui t6, 0xFB00
sw t6, 0x0 (V0)
sw a0, 0x4 (V0)
addiu v0, v0, 0x8
jr ra
sw v0, 0xb06c (AT)


//80405160
//completely reset string

.orga 0x1205160
addiu sp, sp, -0x18
sw ra, 0x14 (SP)
lui at, 0x807f
lh a1, 0x6d20 (AT)
lui t1, 0x8034
lh t1, 0xB944 (T1) //currently playing music
beq a1, t1, endresetstring
nop
jal 0x80320544 //set music
or a0, r0, r0

endresetstring:
jal 0x80402DC0 //cleanup buffer
lui s7, 0x807f
jal 0x80404dd0 //flush string
nop
jal 0x80404da8 //end string
nop
addiu at, s7, 0x6d00
addiu t9, at, 0x100
clear1:
sw r0, 0x0 (AT)
bne t9, at, clear1
addiu at, at, 0x4

lw ra, 0x14 (SP)
jr ra
addiu sp, sp, 0x18


//80404ea8
//a0 = location start
//a1 = num of bytes to add to buffer

.orga 0x1204EA8 //add to buffer
li t7, 0x9d
lw at, 0x6d68 (S7) //string start
subu t0, a0, at //offset
lw t1, 0x6d6c (S7) //buffer ptr
addu t1, t1, s7
sb t0, 0x6001 (T1)
srl t0, t0, 8
sb t0, 0x6000 (T1)
or t9, r0, r0
bufferaddloop:
lb t2, 0x0 (A0)
beq t9, a1, endbufferadd
addiu t1, t1, 1
sb t2, 0x6001 (T1)
sb t7, 0x0 (A0)
addiu a0, a0, 1
beq r0, r0, bufferaddloop
addiu t9, t9, 1

endbufferadd:
lw t1, 0x6d6c (S7) //buffer ptr
addiu a1, a1, 2
addu t1, a1, t1
jr ra
sw t1, 0x6d6c (S7)



//80402DC0
.orga 0x1202DC0 //cleanup buffer
li t7, 0x9d //padding byte
or t3, r0, s7
lw at, 0x6d68 (T3) //string start
checknextaddress:
lbu t1, 0x6000 (T3)
lbu t2, 0x6001 (T3)
sll t1, t1, 8
addu t1, t1, t2
beq t1, r0, bufferfullclean
addu t2, t1, at //ptr to load string from
sb r0, 0x6000 (T3)
sb r0, 0x6001 (T3)
addiu t3, t3, 2
removepaddingloop:
lbu t9, 0x0 (T2)
bne t9, t7, checknextaddress
lbu t8, 0x6000 (T3)
sb t8, 0x0 (T2)
sb r0, 0x6000 (T3)
addiu t3, t3, 1
beq r0, r0, removepaddingloop
addiu t2, t2, 1

bufferfullclean:
jr ra
sw r0, 0x6d6c (S7)
